﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.DataLayer;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmNganHang_TaiKhoan : Form
    {
        private NganHangManagement nganhangMgt = new NganHangManagement();
        private NganHang nganhang = new NganHang();
        private bool isEdited = false;
        public FrmNganHang_TaiKhoan()
        {
            InitializeComponent();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmNganHang_TaiKhoan_Load(object sender, EventArgs e)
        {
            groupNganHangTaiKhoan.Enabled = false;
            BindingToGrid();
            btnSua.Enabled = false;
            btnLuu.Enabled = false;

        }
        public void BindingToGrid()
        {
            ResultObj result = new ResultObj();
            result = nganhangMgt.GetAllNganHang();
            List<NganHang> listNganHang = (List<NganHang>)result.Result;
            dataGridView1.DataSource = listNganHang;
        }
        public void ChangeStatusTextBox(bool IsReadOnly)
        {
            if (IsReadOnly)
            {
                txtMaNganHang.ReadOnly = true;
                txtTenNganHang.ReadOnly = true;
                txtDiaChi.ReadOnly = true;
            }
            else
            {
                txtMaNganHang.ReadOnly = false;
                txtTenNganHang.ReadOnly = false;
                txtDiaChi.ReadOnly = false;
            }
        }
        public void clearTextbox()
        {
            txtMaNganHang.Clear();
            txtTenNganHang.Clear();
            txtDiaChi.Clear();

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
            clearTextbox();
            groupNganHangTaiKhoan.Enabled = true;
            ChangeStatusTextBox(false);
            txtMaNganHang.Select();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtMaNganHang.Text == "" || txtTenNganHang.Text == "" || txtDiaChi.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin","Thông báo");
                return;
            }
            ResultObj result = new ResultObj();
            string sMaNganHang = txtMaNganHang.Text;
            string sTenNganHang = txtTenNganHang.Text;
            string sDiaChi = txtDiaChi.Text;
            NganHang nganhang = new NganHang();
            nganhang.MaSo = sMaNganHang;
            nganhang.Ten = sTenNganHang;
            nganhang.DiaChi = sDiaChi;
            if (!isEdited)
            {
                result = nganhangMgt.SaveNewNganHang(nganhang);
            }
            else
            {
                nganhang.Id = Convert.ToInt32(lblId.Text);
                result = nganhangMgt.UpdateNganHang(nganhang);
                isEdited = true;
            }
            if (result.ReturnCode > 0)
            {
                MessageBox.Show("Lưu Thành Công", "Thông báo");
                FrmNganHang_TaiKhoan_Load(null, null);
            }
            else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
            {
                MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                txtMaNganHang.Select();
            }
            else
            {
                MessageBox.Show("Thất bại");
                txtMaNganHang.Select();
            }

            BindingToGrid();

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                ChangeStatusTextBox(true);
                btnSua.Enabled = true;
                btnLuu.Enabled = false;
                lblId.Enabled = false;
                lblId.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                txtMaNganHang.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                txtTenNganHang.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                txtDiaChi.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
            }
       }

        private void btnSua_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = true;
            isEdited = true;
            groupNganHangTaiKhoan.Enabled = true;
            ChangeStatusTextBox(false);
            txtMaNganHang.Select();
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            NganHang nganhang = (NganHang)dataGridView1.CurrentRow.DataBoundItem;
            FrmTaiKhoanNH frm = new FrmTaiKhoanNH();
            frm.NganHang = nganhang;
            frm.Show();

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

      
    }
}
